package com.xuwang.controller;

import com.xuwang.Util.JWTutil;
import com.xuwang.Util.TableResult;
import com.xuwang.Util.WriteJson;
import com.xuwang.pojo.*;
import com.xuwang.service.RestaurantService;
import io.swagger.annotations.*;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @ Author  460503919
 * @ Date    2021/9/13 15:45
 * @ VERSION 1.0.0
 */
@Controller
@RequestMapping("/restaurantController")
@Api(value = "(Restaurant)管理", tags = "(Restaurant)管理接口API")
@ApiResponses({
        @ApiResponse(code = 400, message = "请求参数不完整或者错误"),
        @ApiResponse(code = 302, message = "临时重定向，被拦截器栏了"),
        @ApiResponse(code = 304, message = "缓存数据"),
        @ApiResponse(code = 404, message = "找不到页面"),
        @ApiResponse(code = 405, message = "请求方式不正确，比如后台接收是post，但前端请求的是get"),
        @ApiResponse(code = 406, message = "页面地址和接口名冲突"),
        @ApiResponse(code = 415, message = "请求格式不正确，前端可能没有把请求头修改为json，也可能前端请求的json对象没有转换为字符串"),
        @ApiResponse(code = 500, message = "后台服务逻辑错误")
})
public class RestaurantController {

    /**
     * 服务对象
     */
    @Resource
    private RestaurantService restaurantService;

    @Resource
    private PublicInfoController publicInfoController;

    /**
     * 查询餐馆信息
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/showIndex", produces = "application/json;charset=UTF-8")
    @ResponseBody
    @ApiOperation(value = "展示餐馆信息", notes = "登录成功之后展示餐馆信息", httpMethod = "POST", response = String.class)
    public String showIndex(HttpServletRequest request) throws Exception {
       String token=request.getHeader("Authorization");
        TableResult<List> result;
        result = restaurantService.showIndex(token);
        return WriteJson.getJson(result);
    }

    /**
     * 展示食物信息
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/showFood", produces = "application/json;charset=UTF-8")
    @ResponseBody
    @ApiOperation(value = "展示食物信息", notes = "展示食物信息", httpMethod = "GET", response = String.class)
    public String showFood() throws Exception {
        TableResult<List> result;
        result = restaurantService.showFood();
        return WriteJson.getJson(result);
    }

    /**
     * 排行展示
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/showRank", produces = "application/json;charset=UTF-8")
    @ResponseBody
    @ApiOperation(value = "排行展示", notes = "排行展示", httpMethod = "GET", response = String.class)
    public String showRank() throws Exception {
        TableResult<List> result;
        result = restaurantService.showRank();
        return WriteJson.getJson(result);
    }

    /**
     * 添加食品
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/insertFood", produces = "application/json;charset=UTF-8")
    @ResponseBody
    @ApiOperation(value = "添加食品", notes = "添加食品", httpMethod = "POST", response = String.class)
    public String insertFood(@RequestBody RestaurantFood restaurantFood,HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String username = JWTutil.getUserName(token);
        TableResult<String> result;
        result = restaurantService.insertFood(restaurantFood, username);
        return WriteJson.getJson(result);
    }

    /**
     * 展示普通用户信息和历史记录
     */
    @RequestMapping(value = "/showMyInfo", produces = "application/json;charset=UTF-8")
    @ResponseBody
    @ApiOperation(value = "展示普通用户信息和历史记录", notes = "展示普通用户信息和历史记录", httpMethod = "POST", response = String.class)
    public String showMyInfo(HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        TableResult<List> result;
        result = restaurantService.showMyInfo(token);
        return WriteJson.getJson(result);
    }

    /**
     * 点击菜品详情查看菜品所有点评
     */
    @RequestMapping(value = "/showOneFood", produces = "application/json;charset=UTF-8")
    @ResponseBody
    @ApiOperation(value = "点击菜品详情查看菜品所有点评", notes = "点击菜品详情查看菜品所有点评", httpMethod = "POST", response = String.class)
    public String showOneFood(Integer id) throws Exception {
        TableResult<RestaurantFood> result;
        result = restaurantService.showOneFood(id);
        return WriteJson.getJson(result);
    }

    /**
     * 给予点评
     */
    @RequestMapping(value = "/insertComment", produces = "application/json;charset=UTF-8")
    @ResponseBody
    @ApiOperation(value = "给予点评", notes = "给予点评", httpMethod = "POST", response = String.class)
    public String insertComment(@RequestBody Comment comment, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String username = JWTutil.getUserName(token);
        TableResult<String> result;
        result = restaurantService.insertComment(comment, username);
        return WriteJson.getJson(result);
    }
    /**
     * 修改菜品图片信息
     */
    @RequestMapping(value = "/uploadFoodImgInfo", produces = "application/json;charset=UTF-8")
    @ResponseBody
    @ApiOperation(value = "修改菜品图片信息", notes = "修改菜品图片信息", httpMethod = "POST", response = String.class)
    public String uploadFoodImgInfo(Integer id, @Validated FileBean fileBean, HttpServletRequest request) throws Exception {
        //获取图片路径
        String src = publicInfoController.fileUpload(fileBean,request);
        TableResult<String> result;
        result = restaurantService.uploadFoodInfo(id,src);
        return WriteJson.getJson(result);
    }

    /**
     * 修改菜品基础信息
     */
    @RequestMapping(value = "/uploadFoodOtherInfo", produces = "application/json;charset=UTF-8")
    @ResponseBody
    @ApiOperation(value = "修改菜品基础信息", notes = "修改菜品基础信息", httpMethod = "POST", response = String.class)
    public String uploadFoodOtherInfo(@RequestBody RestaurantFood restaurantFood) throws Exception {
        TableResult<String> result;
        result = restaurantService.uploadFoodOtherInfo(restaurantFood);
        return WriteJson.getJson(result);
    }


    /**
     * 修改餐馆图片信息
     */
    @RequestMapping(value = "/uploadRestaurantImgInfo", produces = "application/json;charset=UTF-8")
    @ResponseBody
    @ApiOperation(value = "修改餐馆图片信息", notes = "修改餐馆图片信息", httpMethod = "POST", response = String.class)
    public String uploadRestaurantImgInfo(Integer id, @Validated FileBean fileBean, HttpServletRequest request) throws Exception {
        //获取图片路径
        String src = publicInfoController.fileUpload(fileBean,request);
        TableResult<String> result;
        result = restaurantService.uploadRestaurantImgInfo(id,src);
        return WriteJson.getJson(result);
    }

    /**
     * 修改餐馆基础信息
     */
    @RequestMapping(value = "/uploadRestaurantOtherInfo", produces = "application/json;charset=UTF-8")
    @ResponseBody
    @ApiOperation(value = "修改餐馆基础信息", notes = "修改餐馆基础信息", httpMethod = "POST", response = String.class)
    public String uploadRestaurantOtherInfo(@RequestBody Restaurant restaurant) throws Exception {
        TableResult<String> result;
        result = restaurantService.uploadRestaurantOtherInfo(restaurant);
        return WriteJson.getJson(result);
    }
}
